<template>
  <div class="cart">
    <h2>购物车</h2>
    <div v-for="item in cartItems" :key="item.id" class="cart-item">
      {{ item.name }} - ¥{{ item.price }} - 数量：{{ item.quantity }}
      <button @click="decrementQuantity(item.id)">-</button>
      <button @click="incrementQuantity(item.id)">+</button>
    </div>
    <div class="total-price">
      总价：¥{{ totalPrice }}
    </div>
  </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex';

export default {
  name: 'Cart',
  computed: {
    ...mapGetters(['cartItems', 'totalPrice'])
  },
  methods: {
    ...mapActions(['removeFromCart', 'incrementQuantity', 'decrementQuantity'])
  }
}
</script>

<style scoped>
.cart {
  text-align: center;
  margin: 20px;
}

.cart-item {
  margin: 10px 0;
}

.total-price {
  font-weight: bold;
  margin-top: 20px;
}

button {
  width: 30px;
  height: 30px;
  margin-left: 10px;
}
</style>